<?php
class Administrator_ProductManufactureController extends AdministratorController implements IController{
	/**
	 * Model.
	 * @var Administrator_ProductManufacture_Model
	 */
	protected $_model;
	
	public function validateData($data) {
		$result = array();
		$errors = array();
		if($data['name'] == null ||$data['name'] == ""){
			array_push($errors, "Bạn phải nhập tên");
		}
		if(count($errors)>0){
			$result['isError'] = true;
			$result['errors'] = $errors;
		}
		else {
			$result['isError'] = false;
			$result['errors'] = null;
		}
		$result['entity'] = $data;
		return $result;
	}
	public function beforeSave($obj) {
		$productManu = $this->_model->getByID($obj);
		if(isset($_FILES['ProductManufacture_image']) && $_FILES['ProductManufacture_image']["size"]>0){
			$imageName = $obj[ProductManufacture::NAME] + $obj[ProductManufacture::ID];
			$obj[ProductManufacture::IMAGE] = $this->uploadImage(ProductManufacture::IMAGE, $imageName);
		}
		else{
			if(isset($productManu))
				$obj[ProductManufacture::IMAGE] = $productManu[ProductManufacture::IMAGE];
		}
		return $obj;
	}
	/* (non-PHPdoc)
	 * @see AdministratorController::getSearchQuery()
	*/
  	protected function getSearchQuery($searchEntity) {
		$sql = 'SELECT t.id, t.name,t.address,t.image, t.email, t.website
				FROM productmanufacture t
				 where t.id != 0';
		if(isset($searchEntity[Constant::ORDERBY])){
			$sql .=" order by " . $searchEntity[Constant::ORDERBY];
			if(isset($searchEntity[Constant::ORDERAS]))
				$sql .= " " . $searchEntity[Constant::ORDERAS];
			else
				$sql .= " ASC";
		}
		$page = $searchEntity[Constant::PAGE];
		$size = $searchEntity[Constant::SIZE];
		$sql .= " limit " . (($page - 1) * $size) . "," . $size;
		return $sql;
	}  
	
	/* (non-PHPdoc)
	 * @see AdministratorController::getCountQuery()
	*/
	protected function getCountQuery($searchEntity) {
		return 'SELECT count(t.id) as '.Constant::TOTAL.' FROM productmanufacture t where t.id != 0';
	}
	
	/* (non-PHPdoc)
	 * @see AdministratorController::serializeSearchEntity()
	*/
	protected function serializeSearchEntity() {
		$searchEntity = parent::serializeSearchEntity();
		if(!isset($searchEntity[Constant::ORDERBY]))
			$searchEntity[Constant::ORDERBY] = ProductManufacture::NAME;
		return $searchEntity;
	}
}